$(function () {
    //获取搜索框的焦点
    $(".header-center-box>input").focus(function () {
        $(".header-in").addClass("active")
        $(".header-container").show()
        //处理搜索历史
        $(".history-bottom>li").remove()
        let historyArray = getHistory()
        if (historyArray.length === 0) {
            $(".search-history").hide()
        } else {
            $(".search-history").show()
            historyArray.forEach(function (item) {
                let oLi = $("<li>" + item + "</li>")
                $(".history-bottom").append(oLi)
            })
            $(".history-bottom").click(function () {
                window.location.href = "./../searchDetail/index.html?keyword=" + $(this).text()
            })
        }
        searchScroll.refresh();
    })
    //取消按钮
    $(".harder-cancle").click(function () {
        $(".header-in").removeClass("active")
        $(".header-container").hide()
        $(".header-center-box>input")[0].oninput()
    })
    $(".header-switch>span").click(function () {
        $(this).addClass("active").siblings().removeClass("active")
        $(".header-switch>i").animate({ left: this.offsetLeft }, 100)
    })
    //关闭广告
    $(".search-ad>span").click(function () {
        $(".search-ad").remove()
    })

    //失去搜索框的焦点
    $(".header-center-box>input").blur(function () {
        if (this.value.length === 0) {
            return
        }
        setHistroy(this.value)
        this.value = ""

    })
    // 删除历史搜索信息
    $(".history-top>img").click(function () {
        localStorage.removeItem("history")
        $(".search-history").hide()
    })
    
    //3.处理热搜榜
    HomeApis.getHomeHotDetail()
        .then(function (data) {
            let html = template('hotDetail', data)
            $(".hot-bottom").html(html)
            $(".hot-right .hot-title").forEach(function (ele) {
                $clamp(ele, { clamp: 1 })
            })
            searchScroll.refresh();
        })
        .catch(function (err) {
            console.log(err);
        })
    //4.创建滚动
    let searchScroll = new IScroll('.header-container', {
        mouseWheel: false,
        scrolbars: false,
        /* 
        需要使用is.croll-probe.js才能生效probeType:
        1滚动不繁忙的时候触发
        2滚动时每隔一定时间触发
        3每滚动一像素触发一次
    
        */
        probeType: 3
    })
    // 5.处理相关搜索界面
    $(".header-center-box>input")[0].oninput = throttle(function () {
        if (this.value.length === 0) {
            $(".search-ad").show()
            // $(".search-history").show()
            $(".search-hot").show()
            $(".search-current").hide()
        } else {
            $(".search-ad").hide()
            $(".search-history").hide()
            $(".search-hot").hide()
            $(".search-current").show()
            HomeApis.getHomeSearchSuggest(this.value)
                .then(function (data) {
                    $(".current-bottom>li").remove();
                    data.result.allMatch.forEach(function (obj) {
                        let oLi = $(`
                    <li>
                        <img src="./../common/images/topbar-it666-search.png" alt="">
                        <p>${obj.keyword}</p>
                    </li>`)
                        $(".current-bottom").append(oLi)
                    })
                    $(".current-bottom>li").click(function () {
                        setHistroy($(this).text())
                        window.location.href = "./../searchDetail/index.html?keyword=" + $(this).text()
                    })
                    searchScroll.refresh();

                })
                .catch(function (err) {
                    console.log(err);
                })
        }
        $(".current-top").text(`搜索"${this.value}"`)
        searchScroll.refresh();
    }, 500)

    function getHistory() {
        let historyArray = localStorage.getItem("history")
        if (!historyArray) {
            historyArray = []
        } else {
            historyArray = JSON.parse(historyArray)
        }
        return historyArray
    }
    function setHistroy(value) {
        let historyArray = getHistory()
        if (!historyArray.includes(value)) {
            historyArray.unshift(value)
            localStorage.setItem("history", JSON.stringify(historyArray))
        }
    }
})